package com.pachong.android.baseuicomponent.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.pachong.android.baseuicomponent.activity.swipeback.SwipeBackActivityHelper;
import com.pachong.android.baseuicomponent.activity.swipeback.SwipeBackable;
import com.pachong.android.baseuicomponent.view.CenterToolbar;

import butterknife.ButterKnife;


/**
 * 1. Activity定义了许多的状态，用户可以通过set(status)来切换状态，每个状态对应一个View，用户还可以自定义这个View
 * <p>
 * 每个状态对应一个UI 布局,用户可以自定义这些布局,通过setXX方法（e.g setEmptyView）
 * <p>
 * 2. 提供了title的控制,User可以使用Toolbar,也可以自定义自己的tool bar, Activity默认显示toolbar
 *
 * @author Vivi Peng
 * @version 1.0.0
 * @e-mail pengveivei@sina.cn
 * @QQ 897058316
 * @since 1979-01-01
 */
public abstract class BaseActivity extends StateActivity implements SwipeBackable {
    private SwipeBackActivityHelper mSwipeHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCustomToolbar(new CenterToolbar(this));
        getCustomToolbar().setTitle(getTitle());
        ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        getCustomToolbar().setTitle(title);
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        getCustomToolbar().setTitle(titleId);
    }

    @Override
    public CenterToolbar getCustomToolbar() {
        return (CenterToolbar) super.getCustomToolbar();
    }

    public ImageButton enableBackButton() {
        return getCustomToolbar().enableBackButton();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (mSwipeHelper != null)
            mSwipeHelper.onPostCreate();
    }

    @Override
    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v == null && mSwipeHelper != null)
            return mSwipeHelper.findViewById(id);
        return v;
    }

    @Override
    public void setSwipeBackEnable(boolean enable) {
        if (enable) {
            mSwipeHelper = new SwipeBackActivityHelper(this);
            mSwipeHelper.onActivityCreate();
        }
    }

}
